package com.lyc.platform.common.core.util;

import com.lyc.platform.common.core.constant.CommonConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 返回信息主体
 *
 * @author liangyucheng
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Data
//set返回自身链式对象
@Accessors(chain = true)
public class PlatformResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;

    public static <T> PlatformResponse<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static <T> PlatformResponse<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> PlatformResponse<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> PlatformResponse<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> PlatformResponse<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> PlatformResponse<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> PlatformResponse<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static <T> PlatformResponse<T> restResult(T data, int code, String msg) {
        PlatformResponse<T> response = new PlatformResponse<>();
        response.setCode(code).setData(data).setMsg(msg);
        return response;
    }
}
